package info.wegosoft.android.activity;

import info.wegosoft.R;
import info.wegosoft.android.model.MBlog;
import info.wegosoft.android.model.Platform;
import info.wegosoft.android.qauth.sina.OAuthConstant;
import info.wegosoft.android.qauth.sina.Weibo;
import info.wegosoft.android.qauth.sina.WeiboException;
import info.wegosoft.android.qauth.tencent.OAuth;
import info.wegosoft.android.qauth.tencent.QWeiboAsyncApi;
import info.wegosoft.android.qauth.tencent.QWeiboType.ResultType;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

/**  
 * Filename:    CommentMBlogActivity.java 
 * Package:     info.wegosoft.android.activity 
 * Description: 评论Activity
 * Copyright:   Copyright (c) wegosoft.info 2011  
 * @author:     李辉 
 * email:       fzlihui@gmail.com
 * @version:    1.0  
 * Create at:   2011-6-21 下午10:57:02  
 */
public class CommentMBlogActivity extends Activity {

	private MBlog mblog;
	private EditText microBlog_ed;
	private TextView text_limit;
	private ProgressDialog progressDialog = null;
	private int platformType;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.write_mblog);
		
		mblog = (MBlog) getIntent().getSerializableExtra("mblog");
		
		platformType = this.getSharedPreferences("platform",Context.MODE_PRIVATE).getInt("type", -1);
		
		//字数限制提醒
        text_limit = (TextView) findViewById(R.id.text_limit);
        
		microBlog_ed = (EditText) findViewById(R.id.microBlog_ed);
		
		microBlog_ed.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
			
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			
			public void afterTextChanged(Editable s) {

				int charNum = microBlog_ed.getText().length();
				
				text_limit.setText("" + (140 - charNum));
			}
		});

		TextView titleTV = (TextView) findViewById(R.id.titleTV);
		
		Button sendBtn = (Button) findViewById(R.id.sendBtn);
		
		//腾讯微博评论叫点评，只能评论到最原始的那条，而且需要拼接评价内容
		if(platformType == Platform.TENCENT_PLATFORM){
			
			titleTV.setText(getResources().getString(R.string.comment_title_tencent));
			
			sendBtn.setText(getResources().getString(R.string.send_btn_comment_tencent));
			
			microBlog_ed.setText(" || @" + mblog.getScreen_name() + ": " + mblog.getText());
			
			int charNum = microBlog_ed.getText().length();
				
			text_limit.setText("" + (140 - charNum));
			
		}else{
			
			titleTV.setText(getResources().getString(R.string.comment_title));
			
			sendBtn.setText(getResources().getString(R.string.send_btn_comment));
			
		}
		
		ImageButton insertImageButton = (ImageButton) findViewById(R.id.insert_pic_btn);;
		
		insertImageButton.setVisibility(View.GONE);

		
		
		sendBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				final String text = microBlog_ed.getText().toString().trim();

				if(text.length() == 0){
					
					Toast.makeText(CommentMBlogActivity.this, "说点什么吧", Toast.LENGTH_SHORT).show();
					
				}else{
					progressDialog = ProgressDialog.show(CommentMBlogActivity.this,null,"评论中",true);
					
					new Thread(){

						public void run() {
							
							Looper.prepare(); 
							
							switch (platformType) {
								case Platform.SINA_PLATFORM:
									 
									commentToSina(text);
									 
									 break;
								case Platform.TENCENT_PLATFORM:
									 
									 commentToTencent(text);
									 
									 break;	
								default:
									 break;
							}

							Looper.loop();	
						}	
						
					}.start();					
				}
			}
		});
	}
	
	private void commentToTencent(String text){
		
		SharedPreferences preference = getSharedPreferences("tencent_qauth",Context.MODE_PRIVATE);
		
		QWeiboAsyncApi api = new QWeiboAsyncApi();

		if(api.commentMsg(OAuth.AppKey, OAuth.AppSecret,preference.getString("AccessToken", null),preference.getString("AccessTokenSecret", null), text, ResultType.ResultType_Json, mblog.getId())){
			
			progressDialog.dismiss();
			
			Toast.makeText(CommentMBlogActivity.this, "评论成功", Toast.LENGTH_SHORT).show();
			
			returnToDetailMBlogActivity();
			
		}else{
			
			progressDialog.dismiss();
			
			Toast.makeText(CommentMBlogActivity.this, "评论失败", Toast.LENGTH_SHORT).show();
			
			returnToDetailMBlogActivity();
		}
		
	}
	
	private void commentToSina(String text){
		
		try {

			Weibo weibo = OAuthConstant.getInstance().getWeibo();	
			
			weibo.updateComment(text, mblog.getId(),null);
		
			progressDialog.dismiss();
			
			Toast.makeText(CommentMBlogActivity.this, "评论成功", Toast.LENGTH_SHORT).show();
			
			returnToDetailMBlogActivity();
			
		} catch (WeiboException e) {
			
			progressDialog.dismiss();
			
			Toast.makeText(CommentMBlogActivity.this, "评论失败", Toast.LENGTH_SHORT).show();
			
			e.printStackTrace();
			
			returnToDetailMBlogActivity();
		}
		
	}
	
	private void returnToDetailMBlogActivity(){
		
		Intent intent = new Intent();
		
		Bundle bundle = new Bundle();
		
		bundle.putSerializable("mblog", mblog);
		
		intent.putExtras(bundle);
		
		intent.setClass(CommentMBlogActivity.this, DetailMBlogActivity.class);
		
		CommentMBlogActivity.this.finish();
		
		startActivity(intent);
		
	}
}
